﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace TreasureJunk.pages
{
    public partial class Support : System.Web.UI.Page
    {
        readonly string connectionString = ConfigurationManager.ConnectionStrings["OBSConnectionString"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnExecute.Click += new EventHandler(btnExecute_Click);
        }

        void btnExecute_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                using (SqlCommand comm = new SqlCommand(this.txtQuery.Text, con))
                {
                    switch (this.txtQuery.Text.Trim().Substring(0, 2))
                    {
                        case "IN": // Insert, Update, Delete
                        case "UP":
                        case "DE":
                            int i = comm.ExecuteNonQuery();
                            if (i > 0)
                            {
                                this.litStatus.Text = "Executed: " + i;
                            }
                            break;
                        case "SE": // Select
                            using (var adapter = new SqlDataAdapter(comm))
                            {
                                DataTable table = new DataTable();
                                adapter.Fill(table);
                                this.grdData.DataSource = table;
                                this.grdData.DataBind();
                            }
                            break;
                    }
                }
                con.Close();
            }
        }
    }
}